import Vue from 'vue'
import Router from 'vue-router'
import Index from '@/views/index'
import Home from '@/views/index/index'
import '@daelui/vdog/dist/business/business.css'
import { Four0Four } from '@daelui/vdog/dist/business/business.umd.min.js'
import config from '@/micros/config'

// 判断环境是否是微应用打开
let microPath = ''
if (window.__POWERED_BY_QIANKUN__) {
  microPath = config.microPath
}

// 导航
const Nave = () => import(/* webpackChunkName: "nave" */ '@/views/nave')
const NaveList = () => import(/* webpackChunkName: "nave" */ '@/views/nave/list')
const NaveItem = () => import(/* webpackChunkName: "nave" */ '@/views/nave/item')
const NaveEdit = () => import(/* webpackChunkName: "nave" */ '@/views/nave/edit')
const NaveRule = () => import(/* webpackChunkName: "nave" */ '@/views/nave/rule/list')
const NaveEnvirment = () => import(/* webpackChunkName: "nave" */ '@/views/nave/envirment/list')
// 笔记
const Note = () => import(/* webpackChunkName: "note" */ '@/views/note')
const NoteList = () => import(/* webpackChunkName: "note" */ '@/views/note/list')
const NoteItem = () => import(/* webpackChunkName: "note" */ '@/views/note/item')
const NoteEdit = () => import(/* webpackChunkName: "note" */ '@/views/note/edit')
const NoteType = () => import(/* webpackChunkName: "note" */'@/views/note/list-type')
// 任务
const Task = () => import(/* webpackChunkName: "task" */ '@/views/task')
const TaskList = () => import(/* webpackChunkName: "task" */ '@/views/task/list')
const TaskItem = () => import(/* webpackChunkName: "task" */ '@/views/task/item')
const TaskEdit = () => import(/* webpackChunkName: "task" */ '@/views/task/edit')
const TaskType = () => import(/* webpackChunkName: "task" */ '@/views/task/list-type')
// 归档
const Doc = () => import(/* webpackChunkName: "doc" */ '@/views/doc')
const DocList = () => import(/* webpackChunkName: "doc" */ '@/views/doc/list')
const DocItem = () => import(/* webpackChunkName: "doc" */ '@/views/doc/item')
const DocEdit = () => import(/* webpackChunkName: "doc" */ '@/views/doc/edit')
const Data = () => import(/* webpackChunkName: "doc" */ '@/views/data')
const DataList = () => import(/* webpackChunkName: "doc" */ '@/views/data/list')
const DataItem = () => import(/* webpackChunkName: "doc" */ '@/views/data/item')
const DataEdit = () => import(/* webpackChunkName: "doc" */ '@/views/data/edit')

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: microPath + '/',
      name: 'index',
      component: Index,
      redirect: to => {
        return microPath + '/home'
      },
      children: [
        // 首页
        {
          path: 'home',
          name: 'home',
          component: Home
        },
        // 导航
        {
          path: 'nave',
          name: 'nave',
          component: Nave,
          redirect: to => {
            return microPath + '/nave/list'
          },
          children: [
            {
              path: 'list/:id?',
              name: 'nave-list',
              component: NaveList
            },
            {
              path: 'item/:id?',
              name: 'nave-item',
              component: NaveItem
            },
            {
              path: 'edit/:id?',
              name: 'nave-edit',
              component: NaveEdit
            },
            {
              path: 'rule',
              name: 'nave-rule',
              component: NaveRule
            },
            {
              path: 'envirment',
              name: 'nave-envirment',
              component: NaveEnvirment
            }
          ]
        },
        // 笔记
        {
          path: 'note',
          name: 'note',
          component: Note,
          redirect: to => {
            return microPath + '/note/list'
          },
          children: [
            {
              path: 'list',
              name: 'note-list',
              component: NoteList
            },
            {
              path: 'item/:id?',
              name: 'note-item',
              component: NoteItem
            },
            {
              path: 'edit/:id?',
              name: 'note-edit',
              component: NoteEdit
            },
            {
              path: 'type',
              name: 'note-type',
              component: NoteType
            }
          ]
        },
        // 任务
        {
          path: 'task',
          name: 'task',
          component: Task,
          redirect: to => {
            return microPath + '/task/list'
          },
          children: [
            {
              path: 'list',
              name: 'task-list',
              component: TaskList
            },
            {
              path: 'item/:id?',
              name: 'task-item',
              component: TaskItem
            },
            {
              path: 'edit/:id?',
              name: 'task-edit',
              component: TaskEdit
            },
            {
              path: 'type',
              name: 'task-type',
              component: TaskType
            }
          ]
        },
        // 归档
        {
          path: 'doc',
          name: 'doc',
          component: Doc,
          redirect: to => {
            return microPath + '/doc/list'
          },
          children: [
            {
              path: 'list/:dirId?',
              name: 'doc-list',
              component: DocList
            },
            {
              path: 'item/:id?',
              name: 'doc-item',
              component: DocItem
            },
            {
              path: 'edit/:id?',
              name: 'doc-edit',
              component: DocEdit
            }
          ]
        },
        {
          path: 'data',
          name: 'data',
          component: Data,
          redirect: to => {
            return microPath + '/data/list'
          },
          children: [
            {
              path: 'list',
              name: 'data-list',
              component: DataList
            },
            {
              path: 'item/:id?',
              name: 'data-item',
              component: DataItem
            },
            {
              path: 'edit/:id?',
              name: 'data-edit',
              component: DataEdit
            }
          ]
        }
      ]
    },
    {
      path: microPath + '/404',
      name: '404',
      component: Four0Four,
      props: { home: '/home' }
    }
  ]
})

// beforeEach每次进行路由跳转时都会执行
router.beforeEach((to, from, next) => {
  // if (to.matched.length === 0) {
  //   // from.path ? next({name: from.name}) : next('/404')
  //   next(microPath + '/404')
  // } else {
  //   next()
  // }
  next()
})

export default router
